<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/*
 *
 * 栏目管理
 */
class QyAppController extends BaseController {
	//栏目管理首页
	public function index(){
		//搜索条件
		$app_name   = I("app_name",'','trim');
		$qy_name    = I("get.qy_name",'-1','trim');
		$app_admin  = I("app_admin",'','trim');

		$psize = I('get.psize', $this->m_psize,'intval');
		$p = I('get.p');

		
		if(!empty($app_name))
		{
			$where['qy_app.qy_app_name'] = array('like','%'. $app_name . '%' );
		}

		if($qy_name == '-1' || $qy_name == '' && !$_COOKIE['APP_QY_NAME']){
			$q['is_deleted'] = array('eq',0);
			$qy = M("qy")->where($q)->select();
			foreach ($qy as $q => $vq) {
				$q_list[] = $vq['qy_code'];
			}
			$where['qy.qy_code'] = array('in',$q_list);
			setcookie("APP_QY_NAME",'');
		}else{
			if($qy_name != $_COOKIE['APP_QY_NAME'] && !empty($qy_name)){
				$where['qy.qy_code'] = array('eq',$qy_name);
				setcookie("APP_QY_NAME",$qy_name);
			}else{
				$where['qy.qy_code'] = array('eq',$_COOKIE['APP_QY_NAME']);
				setcookie("APP_QY_NAME",$_COOKIE['APP_QY_NAME']);
			}
			
		}
		//列表数据
		$qyapp = M("qy_app");
		$list = $qyapp
				->field("qy_app.*,qy.qy_name")
				->join("LEFT JOIN qy ON qy_app.qy_code = qy.qy_code")
				->where($where)
				->order("qy_app_id desc")
				->select();
		//查询管理员
		foreach ($list as  &$value) 
		{
			$wheres['qy_app_admin.qy_app_id'] = $value['qy_app_id'];
			$value['app_admin']  = M("qy_app_admin")
								->field("qy_app_admin.user_id")
			                    ->where($wheres)
			                    ->select(); 
			 foreach ($value['app_admin'] as &$val)
			 {
			 	$maps['user_id'] = $val['user_id'];
			 	$val = M("user")->field("nickname")->where($maps)->find();
			 }
			 if($value['app_admin'] && count($value['app_admin']) > 0)
			 {
			 	foreach ($value['app_admin'] as  &$v) 
			 	{
			 		$v = implode ('、',$v);
			 	}
			 	$value['app_admin'] = implode('、',$value['app_admin']);
			 }
			 else
			 {
			 	$value['app_admin'] = '';
			 }
		}

		//栏目管理员检索
		if(!empty($app_admin)){
			$map['nickname'] = array('like','%'.$app_admin.'%');
			$user = M("user")->field("user_id")->where($map)->select();
			foreach ($user as $key => $v) {
				$maps['user_id'] = $v['user_id'];
				$arr[] = M("qy_app_admin")->field("qy_app_id")->where($maps)->select();
			}
			if($arr){
				foreach ($arr as $k => $vals) {
					if(!empty($vals)){
						foreach ($vals as $keys => $values) {
							if(!empty($values)){
								$slist[] = $values;
							}
						}
					}		
				}
				if($slist){
					foreach ($slist as $kv => $vs) {
						$app_id[] = $vs['qy_app_id'];
					}
					$listId = array_unique($app_id);
					foreach ($listId as $sId) {
						foreach ($list as $tree ) {
							if($tree['qy_app_id'] == $sId){
								$new[] = $tree;
							}
						}
					}
					if($new){
						$list = $new;
					}else{
						$list = '';
					}	
				}else{
					$list = '';
				}
			}else{
				$list = '';
			}
		}
	  	
	  	//分页计算 
      	$count = count($list);
      	$Page  = new \Common\ORG\Page( $count, $psize );
      	for($i =($p-1)*$psize;$i<$p*$psize;$i++){
          	$new_list[$i] = $list[$i];
       	}
   	 	foreach ($new_list as $v) {
          	if(!empty($v)){
              	$resultData[] = $v;
          	}
       	}
		if($list)
		{
			$res['rcode'] = 1;
			$res['list']  = $resultData;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}
		else
		{
			$res['rcode'] = 2;
		}
		e_json( $res);
	}
	//栏目添加
	// public function add_app(){
	// 	//检验栏目是否存在
	// 	$where['qy_app_name'] = I("post.qy_app_name");
	// 	$where['qy_code'] = I("post.qy_code");
	// 	$qy_app = M("qy_app")->where($where)->find();
		
	// 	if(!$qy_app)
	// 	{
	// 		$map['qy_app_name'] = I("post.qy_app_name");
	// 		$map['qy_code'] = I("post.qy_name");
	// 		//默认参数
	// 		$map['qy_app_logo'] = '';
	// 		$map['agent_id'] = '';
	// 		$app = M("qy_app")->add($map);

	// 		if($app){
	// 			$res['rcode'] = 1;
	// 			$res['msg'] = "栏目添加成功!!!";
	// 		}	
	// 		else
	// 		{
	// 			$res['msg'] = "栏目添加失败!!!";
	// 		}
	// 	}
	// 	else
	// 	{
	// 		$res['msg'] = "此栏目已存在!!!";
	// 	}
	// 	e_json($res);
	// }

	//添加管理員
	public function add_admin(){
		$customer = I("get.customer_id");
		$app_id = I("get.app_id");
		$where['qy_app_id'] = $app_id;
		
		if(!empty($customer)){
			if(strstr($customer,","))
			{		
				$delete = M("qy_app_admin")->where($where)->delete();
				$customer_id = explode(',',$customer);
				foreach ($customer_id as $key => $value) {
					$map['user_id'] = $value;
					$map['qy_app_id'] = $app_id;
					$map['create_time'] = date("Y-m-d H-i-s");

					$add = M("qy_app_admin")->add($map);
				}	
			}elseif(!strstr($customer,",") && !empty($customer)){
				$delete = M("qy_app_admin")->where($where)->delete();
				$map['user_id'] = $customer;
				$map['qy_app_id'] = $app_id;
				$map['create_time'] = date("Y-m-d H-i-s");
				$add = M("qy_app_admin")->add($map);
			}

			if($add){
				$res['rcode'] = 1;
				$res['msg'] = "栏目管理员编辑成功!!!";
			}else{
				$res['msg'] = "栏目管理员编辑失败!!!";
			}
		}else{
			$res['msg'] = "栏目管理员编辑信息内容为空,没有任何更改!!!";
		}
		
		e_json($res);
	}
	//添加套餐
	public function add_package(){
		$package = I("get.packages");
		$app_id = I("get.app_id");
		$where['qy_app_id'] = $app_id;
		if(!empty($package)){
			if(strstr($package,","))
			{	
				$delete = M("qy_app_package")->where($where)->delete();
				$product_package = explode(',',$package);
				foreach ($product_package as $key => $value) {
					$map['package_code'] = $value;
					$map['qy_app_id'] = $app_id;
					$add = M("qy_app_package")->add($map);
				}	
			}elseif(!strstr($package,",") && $package){

				$delete = M("qy_app_package")->where($where)->delete();
				$map['package_code'] = $package;
				$map['qy_app_id'] = $app_id;
				$add = M("qy_app_package")->add($map);
			}
			if($add){
				$res['rcode'] = 1;
				$res['msg'] = "栏目套餐添加成功!!!";
			}else{
				$res['msg'] = "栏目套餐没有更改!!!";
			}
		}else{
			$res['msg'] = "栏目套餐没有更改!!!";
		}
		e_json($res);
	}
	//栏目详情
	public function view_details(){
		$where['qy_app_package.qy_app_id'] = I("qy_app_id");
		$package = M("qy_app_package")
		         ->field("product_package.name,product_package.product_id")
				 ->join("LEFT JOIN product_package ON qy_app_package.package_code = product_package.package_code")
		         ->where($where)
		         ->select();
		foreach ($package as &$value) {
			$wheres['product_id'] = $value['product_id'];
			$product = M("product")->field("name")->where($wheres)->find();
			$value['product_name'] = $product['name'];
		}
		if($package )
		{
			$res['rcode'] = 1;
			$res['package_name'] = $package;
		}
		else
		{
			$res['rcode'] = 2;
		}
		e_json($res);
	}
	//销售人员
	public function sales_peron(){
		if(!$_GET['nickname'] && $_GET['app_id']){
			$p['user.state'] = array('eq',2);
			$p['user.is_deleted'] = array('eq',0);
			$p['qy_app_admin.qy_app_id'] = I("get.app_id");
			$user = M("qy_app_admin")
			            ->field("user.user_id,user.nickname,user.sector_id,user.role_id")
			            ->join("user ON qy_app_admin.user_id = user.user_id")
			            ->where($p)
			            ->select();
			foreach ($user as &$value) {
				//所属分公司
				$wheres['sector_id'] = $value['sector_id'];
				$sector = M("sector")->field("parent_company")->where($wheres)->find();
				if($sector['parent_company'] != 0)
					$maps['sector_id'] = $sector['parent_company'];
				else
					$maps['sector_id'] = 1;
				$company = M("sector")->field("sector_name")->where($maps)->find();
				$value['sector_name'] = $company['sector_name'];
				//所属岗位
				$map['role_id'] = $value['role_id'];
				$role = M("role")->field("role_name")->where($map)->find();
				$value['role_name'] = $role['role_name'];
				$selected_admin[] = $value['user_id'];
			}
		}else{	
			$nickname = $_GET['nickname'];
			$where['user.nickname'] = array('like','%'.$nickname.'%');
			$where['user.state'] = array('eq',2);
			$where['user.is_deleted'] = array('eq',0);

			$user = M("user")
			      ->field("user.user_id,user.nickname,role.role_name,sector.parent_company")
			      ->join("LEFT JOIN role ON user.role_id = role.role_id")
			      ->join("LEFT JOIN sector ON user.sector_id = sector.sector_id")
			      ->where($where)
			      ->select();

			foreach ($user as &$value) {
				if($value['parent_company'] != 0)
					$wheres['sector_id'] = $value['parent_company'];
				else
					$wheres['sector_id'] = 1;

				$sector = M("sector")->field("sector_name")->where($wheres)->find();
				$value['sector_name'] = $sector['sector_name'];
			}
			$res['cccc'] = $user;
		}

		if($_POST['user'] && !empty($user)){
			//处理、合并历史记录
			$list = $_POST['user'];
			foreach ($list as $k=>$v){
				$v=join(',',$v); 
				$temp[$k]=$v;
			}
			$temp=array_unique($temp); 
			foreach ($temp as $k => $v){
				$array=explode(',',$v); 
				$temp2[$k]['user_id'] =$array[0];
				$temp2[$k]['nickname'] =$array[1];
				$temp2[$k]['role_name'] =$array[5];
				$temp2[$k]['sector_name'] =$array[4];
			}

			$arr = array();
			foreach ($temp2 as $keys => $values) {
				if(!in_array($values,$user)){
					$arr[]=$values;
				}
			}
			foreach ($arr as $k=>$v){
				$v=join(',',$v); 
				$temps[$k]=$v;
			}
			$temps=array_unique($temps); 
			foreach ($temps as $k => $v){
				$array=explode(',',$v); 
				$tempes[$k]['user_id'] =$array[0];
				$tempes[$k]['nickname'] =$array[1];
				$tempes[$k]['role_name'] =$array[2];
				$tempes[$k]['sector_name'] =$array[3];
			}

			$list = array_merge($user,$tempes);
			$key = user_id;
			$tmp_arr = array();
			foreach($list as $k => $v)
			{
				if(in_array($v[$key], $tmp_arr))
				{
					unset($list[$k]);
				}
				else 
				{
					$tmp_arr[] = $v[$key];

				}
			}
			//已存在的管理员
			$p['qy_app_admin.qy_app_id'] = I("get.app_id");
			$my_admines = M("qy_app_admin")
		            ->field("user.user_id,user.nickname,user.sector_id,user.role_id")
		            ->join("user ON qy_app_admin.user_id = user.user_id")
		            ->where($p)
		            ->select();
		    $new_list = array_merge($list,$my_admines);
			$key = user_id;
			$tmp_arrs = array();
			foreach($new_list as $k => $v)
			{
				if(in_array($v[$key], $tmp_arrs))
				{
					unset($new_list[$k]);
				}
				else 
				{
					$tmp_arrs[] = $v[$key];
				}
			}
			if($new_list){
				$user = $new_list;
			}else{
				$user = $tempes;
			}	
		}

		if($user)
		{
			$res['rcode'] = 1;
			$res['user']  = $user;
			if($selected_admin) $res['selected_admin'] = $selected_admin;
		}
		else
		{
			$res['msg']  = "销售人员不存在!!!";
		}

		e_json($res);
	}
	//产品
	public function product_package(){
		$where['qy.qy_code'] = I("qy_code");
		$where['product_package.is_deleted'] = array('eq',0);
		$package = M("qy")
				->field("product_package.name,product_package.package_code,product.name as product_name")
				->join("LEFT JOIN product_line ON qy.product_line_code = product_line.product_line_code")
				->join("LEFT JOIN product_package ON product_line.product_line_code = product_package.product_line_code")
				->join("LEFT JOIN product ON product_package.product_id = product.product_id")
				->where($where)
				->select();
		
		$wheres['qy_app_id'] = I("app_id");
		$app_package = M("qy_app_package")->field("package_code")->where($wheres)->select();

		foreach ($package as &$value){
			foreach ($app_package as $k => $val){
				if($val['package_code'] == $value['package_code']){
					$value['my_app'] = 1;
					$selected_app[] = $value['package_code'];
				}
			}
		}
		if($package)
		{
			$res['rcode'] = 1;
			$res['product_package'] = $package;
			if($selected_app) $res['selected_app'] = $selected_app;
		}
		else
		{
			$res['msg'] = "没有找到相关联的套餐!!!";
		}
		e_json( $res);
	}
	//企业号(加载页面检索条件)
	public function productData(){
		$qy_name = isset($_COOKIE['APP_QY_NAME']) ? $_COOKIE['APP_QY_NAME'] : -1;
		$where['is_delete'] = array('neq',1);
		$qy = M("qy")->field("qy_code,qy_name")->where($where)->select();
		$res['product'] = M("product")->field("product_id,name")->select();
		$res['qy_app'] = M("qy_app")->field("qy_app_id,qy_app_name")->select(); 

		$num = count($qy)+1;	    
	    $qy[$num]['qy_code'] = "-1";
	    $qy[$num]['qy_name'] = "全部";
	   	$res['qy'] = array_reverse($qy);
	   	$res['cookie_qy_name'] = $qy_name;
		e_json( $res);
	}
}
